import "package:flutter/material.dart";
import "package:flutter_form_builder/flutter_form_builder.dart";

class FormSelectField extends StatelessWidget {
  final Object? initialValue;
  final String name;
  final TextAlign textAlign;
  final String Function(dynamic item)? itemAsString;

  const FormSelectField({
    super.key,
    this.initialValue,
    required this.name,
    this.textAlign = TextAlign.right,
    this.itemAsString,
  });

  @override
  Widget build(BuildContext context) {
    return FormBuilderField(
      initialValue: initialValue,
      name: name,
      builder: (field) {
        String text = itemAsString == null
            ? (field.value ?? "").toString()
            : itemAsString!(field.value);
        return InputDecorator(
          decoration: const InputDecoration(hintText: "请选择"),
          isEmpty: field.value == null,
          textAlign: textAlign,
          child: Text(text, textAlign: textAlign),
        );
      },
    );
  }
}
